// 登入组件
import React from "react";
import { Button, Form, Input, message } from "antd";
import { LockOutlined, UserOutlined } from '@ant-design/icons';
import {Link, useNavigate} from 'react-router-dom'
import "./less/Login.less";
import logoImg from "../assets/images/logo.png";
import {RegisterApi} from '../utils/request/api'

export default function Register() {
  const navigate = useNavigate()
  const onFinish = (values) => {
    RegisterApi({
      username: values.username,
      password: values.password
    }).then(res=>{
      if(res.errCode === 0){
        message.success(res.message);
        // 跳转到登录页
        setTimeout(()=>{navigate('/login')},1000)
      }else{
        message.warning(res.message);
      }
    })
  };
  const onFinishFailed = (errorInfo) => {
    message.error('注册失败');
    // console.log("Failed:", errorInfo);
  };
  return (
    <div className="login">
      <div className="login_box">
        <img src={logoImg} alt="" />
        <Form
          name="basic"
          labelCol={{
            span: 6,
          }}
          wrapperCol={{
            span: 16,
          }}
          initialValues={{
            remember: true,
          }}
          onFinish={onFinish}
          onFinishFailed={onFinishFailed}
          autoComplete="off"
        >
          <Form.Item
            label="Username"
            name="username"
            rules={[
              {
                required: true,
                message: "请输入用户名!",
              },
            ]}
          >
            <Input prefix={<UserOutlined />} placeholder="请输入用户名"/>
          </Form.Item>
          <Form.Item
            label="Password"
            name="password"
            rules={[
              {
                required: true,
                message: "请输入密码!",
              },
            ]}
          >
            <Input.Password prefix={<LockOutlined/>} placeholder="请输入密码"/>
          </Form.Item>

          <Form.Item
        name="confirm"
        label="确认密码"
        dependencies={['password']}
        hasFeedback
        rules={[
          {
            required: true,
            message: '请再次确认密码!',
          },
          ({ getFieldValue }) => ({
            validator(_, value) {
              if (!value || getFieldValue('password') === value) {
                return Promise.resolve();
              }
              return Promise.reject(new Error('您输入的两个密码不匹配!!'));
            },
          }),
        ]}
      >
        <Input.Password prefix={<LockOutlined/>} placeholder="请再次确认密码"/>
      </Form.Item>

          <Form.Item
            wrapperCol={{
              offset: 6,
            }}
          >
            <Link to="/login">已有账号？前往登录</Link>
          </Form.Item>
          <Form.Item
            wrapperCol={{
              offset: 11,
              span: 16,
            }}
          >
            <Button size="large" type="primary" htmlType="submit">
              立即注册
            </Button>
          </Form.Item>
        </Form>
      </div>
    </div>
  );
}
